package cn.tedu.luban.worker.main.repository;


import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.mapper.WorkerMapper;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.dos.WorkerDO;
import cn.tedu.luban.worker.protocol.param.WorkerParam;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.query.WorkerDBPageQuery;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;


import java.util.List;

@Repository
@Slf4j
public class WorkerRepository {

    @Autowired
    private WorkerMapper workerMapper;

    @Autowired
    private WorkerConverter workerConverter;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String WORKER_CACHE_PREFIX="luban:worker:";

    public void deleteByUserId(Long userId) {
        //调用mapper，删除userId所绑定的师傅
        workerMapper.deleteByUserId(userId);
        //删除缓存：为了保证数据一致性
        redisTemplate.delete(WORKER_CACHE_PREFIX+userId);
    }

    public Long saveWorker(WorkerRegistParam workerRegistParam) {
        //将workerRegistParam 转换成 workerDO对象
        WorkerDO workerDO=workerConverter.param2do(workerRegistParam);
        workerMapper.insert(workerDO);
        redisTemplate.delete(WORKER_CACHE_PREFIX+workerRegistParam.getUserId());
        return workerDO.getId();
    }


    public FrontWorkerBO getWorkerByUserId(Long userId) {

        //1.提供缓存key
        String workerKey = WORKER_CACHE_PREFIX + userId;
        ValueOperations opsForValue = redisTemplate.opsForValue();

        //2.读缓存数据
        FrontWorkerBO cacheWorker =(FrontWorkerBO) opsForValue.get(workerKey);
        if (cacheWorker ==null ||cacheWorker.getId()==null){
            //3.缓存未命中，读取持久层
            WorkerDO workerDO = workerMapper.selectByUserId(userId);
            cacheWorker= workerConverter.do2frontBo(workerDO);
            opsForValue.set(workerKey,cacheWorker);
        }

        return cacheWorker;
    }

    public Long countWorkerQuery(WorkerListQuery workerListQuery) {
        WorkerDBPageQuery workerDBPageQuery=workerConverter.query2dbQuery(workerListQuery);
        return workerMapper.countWorker(workerDBPageQuery);
    }

    public List<AdminWorkerBO> workerPageQuery(WorkerListQuery workerListQuery) {

        WorkerDBPageQuery workerDBPageQuery=workerConverter.query2dbQuery(workerListQuery);

        List<WorkerDO> dos=workerMapper.selectWorkers(workerDBPageQuery);

        return workerConverter.dos2bos(dos);
    }

    public AdminWorkerBO getAdminWorkerByUserId(Long userId) {
        //利用持久层查询workerDO对象
        WorkerDO workerDO = workerMapper.selectByUserId(userId);
        //转换器 转换对象
        return workerConverter.do2bo(workerDO);
    }

    public void updateAuditStatus(WorkerParam workerParam) {
        //1.转换DO
        WorkerDO workerDO = workerConverter.param2do(workerParam);
        //2.调用更新
        workerMapper.updateAuditStatus(workerDO);
        //3.保证数据一致性，删除缓存
        redisTemplate.delete(WORKER_CACHE_PREFIX+workerParam.getUserId());
    }

    public void updateCertStatus(WorkerParam workerParam) {
        WorkerDO workerDO = workerConverter.param2do(workerParam);
        workerMapper.updateCertStatus(workerDO);
        redisTemplate.delete(WORKER_CACHE_PREFIX+workerParam.getUserId());
    }
}
